import 'dart:io';

import 'package:flu_framework/common/base/BaseWidget.dart';
import 'package:flu_framework/common/channel/location/amap_flutter_location.dart';
import 'package:flu_framework/common/utility/permission_request.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

class LocationPage extends BaseWidget {
  const LocationPage({super.key});

  @override
  BaseWidgetState<BaseWidget> getState() => _LocationPageState();
}

class _LocationPageState extends BaseWidgetState<LocationPage> {

  String address = "";
  late AMapFlutterLocation _location;

  @override
  String setPageTitle() => "定位功能";

  @override
  void initState() {
    super.initState();
    _location = AMapFlutterLocation();

  }

  @override
  void dispose() {
    super.dispose();
    _location.destroy();
  }

  @override
  Widget buildContent() {
    return Center(
      child: Column(
        children: [
          ElevatedButton(
            onPressed: (){
              print("country startLocation");
              if(Platform.isIOS){
                _location.startLocation((event){
                  print("country:${event["country"]}");
                });
              }else{
                permissionCheckAndRequest(context, Permission.location, "定位").then((value){
                  if(value) {
                    print("have  permission");
                    // _location.startLocation().listen((event) {
                    //   print("country:${event["country"]}");
                    // });
                    _location.startLocation((event){
                      print("country:${event["country"]}");
                    });
                  }else{
                    print("no  permission");
                  }
                });
              }

            },
            child: Text("开始定位"),
          ),
          ElevatedButton(
            onPressed: (){
              print("country3 stopLocation");
              _location.stopLocation();
            },
            child: Text("关闭定位"),
          )
        ],
      ),
    );
  }

}

